package cs429_project;

import java.io.PrintStream;

/**
 * Primarily intended for use as a demonstration of the way in which this
 * project classifies queries as {@code EXPLICIT}, {@code AMBIGUOUS},
 * {@code SOLVED_AMBIGUOUS}, and {@code BROADER}.  Call the method
 * {@link ClassificationDemo#classifyQueryDemo(java.lang.String,
 * java.lang.String, java.lang.String, int, int, java.io.PrintStream)} with the
 * parameters as specified by the description, and you will see how the queries
 * get classified.
 * @author Peter Ferrans
 */
public class ClassificationDemo {

    /**
     * Demonstrates how this project classifies queries.  The name of the
     * Wikipedia page and type for the query will be printed out.  Then, if the
     * query type is ambiguous, the machine will search the index using the
     * query, retrieve the top results, cluster them with k-means clustering,
     * find the top cluster, and compare it with all of the Wikipedia pages
     * linked to on the disambiguation page for this query.  The one closest to
     * the centroid of the top cluster is the one that will be assigned to the
     * query.
     * @param query
     *    Query to be classified.
     * @param indexName
     *    The name of the index on which this query will search.  This needs to
     *    be a Lucene index, and ideally one which was created using a
     *    {@link org.apache.lucene.analysis.standard.StandardAnalyzer}.
     * @param field
     *    The field of the documents in the index to search.
     * @param maxResults
     *    Maximum number of documents to be used in the clustering, mentioned
     *    above.  In most cases, 100 is a good number for this.
     * @param k
     *    The number of clusters to be used in k-means clustering, mentioned
     *    above.
     * @param stream
     *    The print stream to which the output should be printed.  In most
     *    cases this will be {@code System.out}.
     */
    public static void classifyQueryDemo(String query, String indexName,
            String field, int maxResults, int k, PrintStream stream) {
        QueryAndWiki qaw = new QueryAndWiki(query);
        stream.println("Query: " + qaw.getQueryText());
        stream.println("      Wikipedia page: " + qaw.getWikiPageName());
        stream.println("      Query type:     " + qaw.getQueryType());

        if (qaw.getQueryType().equals(QueryAndWiki.QueryType.AMBIGUOUS)) {
            stream.println("   This query is ambiguous.  We will solve it.");
            try {
                qaw.solveAmbiguous(indexName, field, maxResults, k);
                stream.println("      New Wikipedia page: " +
                        qaw.getWikiPageName());
                stream.println("      New query type:     " +
                        qaw.getQueryType());
            } catch (Exception e) {
                e.printStackTrace(System.err);
            }
        }
    }
}
